package BaseUserController

import (
	"github.com/gin-gonic/gin"
	"learn/app/controllers/cc"
	"learn/app/services/baseUserService"
)

type Role struct {
	cc.BaseController
	ID       int64  `json:"id" one:"gte=0" two:"require,gte=0"`
	RoleName string `json:"role_name" one:"min=1,max=10" label:"角色名称"`                     // 账号
	Status   int8   `json:"status" one:"oneof=1 2" two:"oneof=1 2" label:"状态" default:"1"` // 用户状态：1 正常，2 删除 // todo default 没用
}

func (obj *Role) Add(c *gin.Context) {
	roleStruct := Role{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &roleStruct, "id"); ok == false {
		return
	}
	res, _ := new(baseUserService.Role).Add(obj.ToMap(roleStruct))
	cc.Return_with_code(res, c)
}

func (obj *Role) Update(c *gin.Context) {
	userStruct := Role{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &userStruct); ok == false {
		return
	}
	res, _ := new(baseUserService.Role).Update(obj.ToMap(userStruct))
	cc.Return_with_code(res, c)
}

func (obj *Role) List(c *gin.Context) {
	res, count := new(baseUserService.Role).List()
	cc.Return_success(c, cc.WithData(res), cc.WithCount(count))
}

func (obj *Role) Del(c *gin.Context) {
	userStruct := Role{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &userStruct, "id"); ok == false {
		return
	}

	res, _ := new(baseUserService.Role).Del(obj.ToMap(userStruct))
	cc.Return_with_code(res, c)
}
